package com.yumeng.framework.core.base.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.RandomUtil;
import com.yumeng.common.api.ApiResult;
import com.yumeng.common.data.annotations.OperationLogIgnore;
import com.yumeng.common.utils.UUIDUtils;
import com.yumeng.common.cache.SimpleCacheAdapter;
import com.yumeng.framework.core.base.vo.CaptchaVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wxd
 * @date 2025/3/28 16:11
 */
@RestController
@RequestMapping("/ym/comm")
@OperationLogIgnore
@Tag(name = "通用Controller", description = "通用功能的接口集合")
@Slf4j
public class YumengCommonController {

    @Operation(summary = "获取验证码")
    @GetMapping("/getCaptcha.do")
    public ApiResult getCaptcha(HttpServletRequest request, HttpServletResponse response) {
        String captchaId = UUIDUtils.getUuidNoFlag();
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 40,4,50);
        String code = lineCaptcha.getCode();
        SimpleCacheAdapter.setCaptcha(captchaId, code);
        log.debug("生成验证码：{}_{}", captchaId, code);
        CaptchaVo vo = new CaptchaVo();
        vo.setCaptchaId(captchaId);
        vo.setCaptchaImage(lineCaptcha.getImageBase64Data());
        return ApiResult.successData(vo);
    }

    //模拟手机号发送验证码
    @Operation(summary = "发送短信验证码")
    @GetMapping("/sendSmsCode.do")
    public ApiResult sendSmsCode(@RequestParam String phone) {
        String code = RandomUtil.randomNumbers(6);
        SimpleCacheAdapter.setSmsCode(phone, code);
        String msg = String.format("【模拟】手机号：%s，已发送验证码：%s", phone, code);
        log.info(msg);
        return ApiResult.successData(msg);
    }

}
